package com.dmooo.ymx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.dmooo.ymx.R;
import com.dmooo.ymx.adapter.TodayHighlightsChildAdapter3;
import com.dmooo.ymx.base.BaseActivity;
import com.dmooo.ymx.bean.Zhujibean;
import com.dmooo.ymx.common.SPUtils;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;
import com.google.gson.Gson;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public class ZhujiActivity1 extends BaseActivity {

    private LinearLayout ly_back;
    private RecyclerView recyclerView;
    private SmartRefreshLayout smartRefreshLayout;
    private TodayHighlightsChildAdapter3 todayHighlightsChildAdapter3;
    @Override
    protected void initUI() {
        setContentView( R.layout.activity_zhuji);
    }

    @Override
    protected void initData() {
        smartRefreshLayout = findViewById( R.id.refresh_layout );
        ly_back = findViewById( R.id.zhuji_back );
        recyclerView = findViewById( R.id.zhuji_recy );


        todayHighlightsChildAdapter3 = new TodayHighlightsChildAdapter3(ZhujiActivity1.this, zhujibeans);
        GridLayoutManager gridLayoutManager = new GridLayoutManager( ZhujiActivity1.this, 2 );
        gridLayoutManager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( gridLayoutManager );
        recyclerView.setAdapter( todayHighlightsChildAdapter3 );
        todayHighlightsChildAdapter3.setsubClickListener( new TodayHighlightsChildAdapter3.SubClickListener() {
            @Override
            public void OntopicClickListener(View v, String detail, int posit) {
                Bundle bundle = new Bundle();
                bundle.putString("num_iid", zhujibeans.get( posit ).goods_id);
                Intent intent = new Intent(ZhujiActivity1.this, PromotionDetailsActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        } );
        todayHighlightsChildAdapter3.setOnremoveListnner(new TodayHighlightsChildAdapter3.OnremoveListnner() {
            @Override
            public void ondelect(int i) {
                dialog(zhujibeans.get(i).id);
//                showToast("del"+i);
            }
        });
        getgoodlist();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                PAGE = 1;
//                allgoodlistbeans.clear();
//                getgoodlist("41");
                getgoodlist();
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                PAGE ++;
                getgoodlist();
                //refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败

            }
        });
    }

    @Override
    protected void initListener() {
        ly_back.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        } );
    }
    List<Zhujibean> zhujibeans = new ArrayList<>(  );
    public int PAGE = 1;
    private void getgoodlist() {

        RequestParams requestParams = new RequestParams();
        requestParams.put( "token", SPUtils.getStringData(this, "token", "") );
        requestParams.put("p",PAGE);
        requestParams.put( "per",8 );
        HttpUtils.post1( Constants.APP_IP+"/app.php?c=TbFoot&a=getFootlist", requestParams, new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();

                if (smartRefreshLayout!=null){
                    smartRefreshLayout.finishRefresh();
                    smartRefreshLayout.finishLoadMore();
                }else{
                    showLoadingDialog();
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

                if (smartRefreshLayout != null) {

                }else{
                    closeLoadingDialog();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject=new JSONObject(responseString);
                    if ("0".equals( jsonObject.getString( "code" ) )) {
                        JSONObject object = jsonObject.getJSONObject( "data" );
                        JSONArray array = object.getJSONArray( "list" );
                        if(PAGE==1){
                            zhujibeans.clear();
                        }
                        for (int i = 0; i < array.length(); i++) {
                            zhujibeans.add( new Gson().fromJson( array.getJSONObject( i ).toString(), Zhujibean.class ) );
                        }
                        todayHighlightsChildAdapter3.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public void dialog(String zjid){
        showTipDialog3("温馨提示", "是否确认删除!", new onClickListener() {
            @Override
            public void onClickSure() {
                postdeladdress(zjid);
            }
        }, new onClickListener() {
            @Override
            public void onClickSure() {

            }
        },"确定","取消");
    }

    private void postdeladdress(String zjid) {

        RequestParams requestParams = new RequestParams();
        requestParams.put( "token",SPUtils.getStringData(ZhujiActivity1.this, "token", "") );
        requestParams.put( "id",zjid );
        HttpUtils.post1( Constants.APP_IP+"/app.php?c=TbFoot&a=del", requestParams, new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();

                showLoadingDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject=new JSONObject(responseString);
                    if ("0".equals( jsonObject.getString( "code" ) )) {
                        zhujibeans.clear();
                        PAGE = 1;
                        getgoodlist();
                        showToast(  jsonObject.getString( "msg" ) );
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
